/*
 * CaesarJ Tutorial
 *
 * Copyright 2004. TU-Darmstadt. Software Technology Group 
 */
package gui;

import hierarchydisplay.HierarchyDisplay;
import hierarchydisplay.HierarchyInitData;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import company.Company;

/**
 * Main application class: creates user interface, initializes data.
 */
public class Application {

	/**
	 * State variables
	 */
	private HierarchyView view = new HierarchyView();
	private JFrame frame = new JFrame();
	private Company company = new Company();
	
	/* Entry point to the application */ 
	public static void main(String[] args) {
		new Application().start();
	}

	/* Starts the application by initializing GUI */
	public void start()	{

		/** @todo 1) Replace this block by the block below */
		//HierarchyDisplay hd = new HierarchyDisplay();
		//HierarchyInitData.load(hd);
		//view.setHierarchy(hd);
		//view.repaint();
		//buildInterface();

		/** @todo 1) Scaffolding for setting up company display */
		//Database.load(company);
		//view.setHierarchy(/*new HierachyDiplay type for diplaying the company*/);
		//buildInterface();
		//view.repaint();
	}
	
	/* Build application window */
	private void buildInterface() {
		frame.setSize(800, 600);
		frame.setContentPane(view);
		displayOnScreen(frame);
		frame.setTitle("Java - Hierarchies");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setJMenuBar(buildMenuBar());
		frame.setVisible(true);
	}

	/* Build application menu bar. */
	private JMenuBar buildMenuBar() {
		JMenuBar menuBar = new JMenuBar();
				
		JMenu menu = new JMenu("Change");
		
		menuBar.add(menu);
		
		RandomCompanyChange change = new RandomCompanyChange(view.getHierarchy(),company);
		
		JMenuItem menuItem = new JMenuItem("Transfer Worker");
		menuItem.addActionListener(change);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Rename Worker");
		menuItem.addActionListener(change);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Rename Manager");
		menuItem.addActionListener(change);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Rename CEO");
		menuItem.addActionListener(change);
		menu.add(menuItem);
		
		/** @todo 2.2) Insert menu for random collapse/expand. */ 
		
		return menuBar;
    }
	
		/* Display application window at the screen center. */
	private void displayOnScreen(JFrame frame)	{
		Dimension paneSize   = frame.getSize();
		Dimension screenSize = frame.getToolkit().getScreenSize();
		frame.setLocation(
			(screenSize.width  - paneSize.width)  / 2,
			(screenSize.height - paneSize.height) / 2);
	}	
}